package com.cloud.file.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//使得其实现类能够被classpath扫描自动发现
@RestControllerAdvice
public class ExceptionHandlerAdvice {
	// 统一处理某一类异常，从而能够减少代码重复率和复杂度
	@ExceptionHandler({ IllegalArgumentException.class })
	// 可以将某种异常映射为HTTP状态码
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public Map<String, Object> badRequestException(IllegalArgumentException exception) {
		Map<String, Object> data = new HashMap<>();
		data.put("code", HttpStatus.BAD_REQUEST.value());
		data.put("message", exception.getMessage());

		return data;
	}
}
